/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latesis.web.controller;

import com.latesis.bean.AlertaBean;
import com.latesis.bean.ChoferBean;
import com.latesis.bean.PropietarioBean;
import com.latesis.dao.PropietarioDAO;
import com.latesis.util.LazySorter;
import com.latesis.web.service.MantenimientoService;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Alan - Desarrollo
 */
public class LazyChoferDataModel extends LazyDataModel<ChoferBean> implements Serializable {
    
    
    private List<ChoferBean> lista;
        // Selected Page size in the DataTable
    private int pageSize;
    // Current row index number
    private int rowIndex;
    // Total row number
    private int rowCount;
    private MantenimientoService mantenimientoService;
    
    public LazyChoferDataModel(){}
    
     public LazyChoferDataModel(MantenimientoService crudService) {
        this.mantenimientoService = crudService;
    }
    
    @Override
    public List<ChoferBean> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        System.out.println("load");
            
        setLista(mantenimientoService.buscarCustomChofer(first, first+pageSize));
        if(sortField!=null){
            Collections.sort(lista, new LazySorter(sortField,sortOrder));
        }
        setRowCount(mantenimientoService.countTotalChofer());
        return lista;
    }
    
        /**
     * Checks if the row is available
     * @return boolean
     */
    @Override
    public boolean isRowAvailable() {
        System.out.println("isRowAvailable: " + lista);
        if(lista == null) 
            return false;
        int index = rowIndex % pageSize ; 
        System.out.println("index: " + index + " rowIndex % pageSize: " + rowIndex % pageSize + " rowIndex: " + rowIndex + " pageSize: " + pageSize);
        return index >= 0 && index < lista.size();
    }
    
    /**
     * Gets the user object's primary key
     * @param user
     * @return Object
     */
    @Override
    public Object getRowKey(ChoferBean user) {
        System.out.println("getRowKey");
        return user.getId().toString();
    }

    /**
     * Returns the user object at the specified position in datasource.
     * @return 
     */
    @Override
    public ChoferBean getRowData() {
        System.out.println("getRowData");
        
        if(lista == null)
            return null;
        int index =  rowIndex % pageSize;
        System.out.println("index: " + index + " rowIndex % pageSize: " + rowIndex % pageSize + " rowIndex: " + rowIndex + " pageSize: " + pageSize);
        if(index > lista.size()){
            return null;
        }
        System.out.println(lista.get(index));
        return lista.get(index);
    }
    
    /**
     * Returns the user object that has the row key.
     * @param rowKey
     * @return 
     */
    @Override
    public ChoferBean getRowData(String rowKey) {
        System.out.println("getRowDatarowKey: " + rowKey);
        if(lista == null)
            return null;
       for(ChoferBean user : lista) {  
            System.out.println("user.getId(): " + user.getId() + " ");
           if(user.getId().toString().equals(rowKey))  
           return user;  
       }  
       return null;  
    }

    /**
     * @return the pageSize
     */
    @Override
    public int getPageSize() {
        System.out.println("getPageSize");
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    @Override
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return the rowIndex
     */
    @Override
    public int getRowIndex() {
        return rowIndex;
    }

    /**
     * @param rowIndex the rowIndex to set
     */
    @Override
    public void setRowIndex(int rowIndex) {
        
        this.rowIndex = rowIndex;
    }

    /**
     * @return the rowCount
     */
    @Override
    public int getRowCount() {
        return rowCount;
    }

    /**
     * @param rowCount the rowCount to set
     */
    @Override
    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }
    
        /**
     * Sets wrapped data
     * @param list
     */
    @Override
    public void setWrappedData(Object list) {
        this.setLista((List<ChoferBean>) list);
    }
    
    /**
     * Returns wrapped data
     * @return
     */
    @Override
    public Object getWrappedData() {
        return lista;
    }

    /**
     * @param lista the lista to set
     */
    public void setLista(List<ChoferBean> lista) {
        this.lista = lista;
    }

    /**
     * @param mantenimientoService the mantenimientoService to set
     */
    public void setMantenimientoService(MantenimientoService mantenimientoService) {
        this.mantenimientoService = mantenimientoService;
    }
    
    
    
}
